<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>徐海用车申请系统登录</title>
    <link rel="stylesheet" href="static/layui/css/layui.css">
    <link rel="stylesheet" href="static/css/base.css">
    <script src="static/js/jquery.js"></script>
    <script src="static/js/ajax.js"></script>
    <script src="static/layui/layui.js"></script>
    <style>
        .main {
            height: 100vh;
            width: 100vw;
            overflow: hidden;
            display: flex;
            align-items: center;
            justify-content: center;
            background: url("static/images/login-bg.jpg") no-repeat center/cover;
        }

        .main .login-form {
            width: 350px;
            background-color: rgba(0, 0, 0, .4);
            border-radius: 20px;
        }

        .login-container {
            width: 320px;
            margin: 20px auto;
        }

        .login-other .layui-icon {
            position: relative;
            display: inline-block;
            margin: 0 2px;
            top: 2px;
            font-size: 26px;
        }
    </style>
</head>
<body>
<div class="main">
    <div class="login-form">
        <h2 style="text-align: center;margin-top: 20px">后台登录</h2>
        <form class="layui-form">
            <div class="login-container">
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-username"></i>
                        </div>
                        <input type="text" name="username" value="" lay-verify="required" placeholder="用户名"
                               lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-password"></i>
                        </div>
                        <input type="password" id="password" name="password" value="" lay-verify="required"
                               placeholder="密   码"
                               lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-vercode"></i>
                                </div>
                                <input type="text" id="code" name="code" value="" lay-verify="required"
                                       placeholder="验证码"
                                       lay-reqtext="请填写验证码" autocomplete="off" class="layui-input"
                                       lay-affix="clear">
                            </div>
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 10px;">
                                <img id="kaptcha" src=""
                                     onclick="getKaptcha()">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login">登录</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    let uuid;
    const form = layui.form;
    $(function () {
        getKaptcha()
        form.on("submit(login)", function (data) {
            data.field.uuid = uuid;
            ajax.post('login', data.field).then(() => {
                location.replace("home")
            }).catch(() => {
                getKaptcha();
                $("#code").val("");
                $("#password").val("");
            })
            return false;
        });
    })

    function getKaptcha() {
        ajax.get('kaptcha?r=' + Math.random()).then(data => {
            uuid = data.uuid;
            $("#kaptcha").prop("src", 'data:image/jpeg;base64,' + data.image);
        })
    }
</script>
</html>
